package com.gexiaobao.pigeon.ui.fragment.mine.organization

import android.os.Bundle
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.databinding.FragmentJoinBinding
import com.gexiaobao.pigeon.ui.adapter.OrganizationAdapter
import com.gexiaobao.pigeon.viewmodel.OrganizationViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction

/**
 * created by : hxw
 * @date : 20221123
 * Describe :我加入的
 */
class FragmentJoin() : BaseFragment<OrganizationViewModel, FragmentJoinBinding>() {

    private var mStatus = 0
    private var mOrgId = 0

    private val mAdapter: OrganizationAdapter by lazy { OrganizationAdapter(arrayListOf()) }
    //界面状态管理者
    private lateinit var loadsir: LoadService<Any>

    override fun initView(savedInstanceState: Bundle?) {
        mStatus = requireArguments().getInt("status")
        val layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        mDatabind.layoutList.swipeRecyclerView.init(layoutManager, mAdapter, false)
        //状态页配置
        loadsir = loadServiceInit(mDatabind.layoutList.swipeRefreshLayout) {
            //点击重试时触发的操作
            loadsir.showLoading()
            mViewModel.getOrgJoinList(true, "1", mStatus)
        }

        loadsir.showLoading()
        mViewModel.getOrgJoinList(true, "1", mStatus)

        mAdapter.run {
            setOnItemClickListener { _, _, position ->
                if (mStatus != 1 && mStatus != 2) {
                    mOrgId = mAdapter.data[position].id
                    nav().navigateAction(R.id.action_to_org_info, Bundle().apply {
                        putInt("orgId", mOrgId)
                        putString("type", "org")
                    })
                } else {
                    nav().navigateAction(R.id.action_to_org_detail, Bundle().apply {
                        putParcelable("data", mAdapter.data[position])
                        putInt("type", 1)
                        putInt("position", position)
                    })
                }
            }
        }

        mDatabind.layoutList.swipeRefreshLayout.init {
            //触发刷新监听时请求数据
            mViewModel.getOrgJoinList(true, "1", mStatus)
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.searchOrgData.observe(viewLifecycleOwner) {
            if (it.isSuccess) {
                loadListData(it, mAdapter, loadsir, mDatabind.layoutList.swipeRecyclerView, mDatabind.layoutList.swipeRefreshLayout)
            } else {
                RxToast.showToast(it.errMessage)
            }
        }
    }

    companion object {

        fun newInstance(status: Int): FragmentJoin {
            val fragment = FragmentJoin()
            val bundle = Bundle()
            bundle.putInt("status", status)
            fragment.arguments = bundle
            return fragment
        }
    }

}